CREATE TABLE IF NOT EXISTS `__PREFIX__mental_category`  (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(30) CHARACTER SET utf8mb4 NOT NULL DEFAULT '',
  `weigh` int(10) NOT NULL DEFAULT 0 COMMENT '权重',
  `createtime` int(10) NULL DEFAULT NULL COMMENT '创建时间',
  `updatetime` int(10) NULL DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COMMENT = '分类';

CREATE TABLE IF NOT EXISTS `__PREFIX__mental_config`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '变量名',
  `title` varchar(100) CHARACTER SET utf8mb4 NOT NULL DEFAULT '' COMMENT '变量标题',
  `value` text CHARACTER SET utf8mb4 NOT NULL COMMENT '变量值',
  `t_name` varchar(50) CHARACTER SET utf8mb4 NOT NULL COMMENT '二次名',
  `content` text CHARACTER SET utf8mb4 NOT NULL COMMENT '二次值',
  `type` varchar(30) CHARACTER SET utf8mb4 NOT NULL DEFAULT '' COMMENT '类型:string,text,int,bool,array,datetime,date,file',
  `rule` varchar(100) CHARACTER SET utf8mb4 NOT NULL DEFAULT '' COMMENT '验证规则',
  `tip` varchar(100) CHARACTER SET utf8mb4 NULL DEFAULT NULL,
  `extend` varchar(255) CHARACTER SET utf8mb4 NOT NULL DEFAULT '' COMMENT '扩展属性',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COMMENT = '测试配置';

CREATE TABLE IF NOT EXISTS `__PREFIX__mental_feedback`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NULL DEFAULT NULL,
  `content` varchar(1000) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '内容',
  `createtime` int(10) NULL DEFAULT NULL COMMENT '创建时间',
  `updatetime` int(10) NULL DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COMMENT = '意见反馈';

CREATE TABLE IF NOT EXISTS `__PREFIX__mental_order`  (
  `order_id` varchar(32) CHARACTER SET utf8mb4 NOT NULL DEFAULT '' COMMENT '订单号',
  `type` tinyint(1) UNSIGNED NULL DEFAULT 0 COMMENT '类型:0=付费测试,1=付费查看',
  `main_id` int(10) NULL DEFAULT NULL,
  `test_id` int(10) NULL DEFAULT NULL,
  `user_id` int(10) UNSIGNED NULL DEFAULT NULL COMMENT '用户ID 关联user.id',
  `amount` decimal(10, 2) UNSIGNED NULL DEFAULT 0.00 COMMENT '订单金额',
  `payamount` decimal(10, 2) UNSIGNED NULL DEFAULT 0.00 COMMENT '支付金额',
  `ip` varchar(50) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '下单IP',
  `useragent` varchar(255) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT 'UserAgent',
  `paytype` varchar(50) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '支付类型',
  `paytime` int(10) NULL DEFAULT NULL COMMENT '支付时间',
  `memo` varchar(255) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '备注',
  `status` enum('created','paid','expired') CHARACTER SET utf8mb4 NULL DEFAULT 'created' COMMENT '状态',
  `createtime` int(10) NULL DEFAULT NULL COMMENT '添加时间',
  `updatetime` int(10) NULL DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`order_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COMMENT = '订单';

CREATE TABLE IF NOT EXISTS `__PREFIX__mental_question`  (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `type` enum('skip','mark') CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '类型:skip=跳转,mark=打分',
  `title` varchar(255) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '标题',
  `image` varchar(100) CHARACTER SET utf8mb4 NOT NULL DEFAULT '' COMMENT '图片',
  `category_id` int(10) NULL DEFAULT NULL,
  `question` text CHARACTER SET utf8mb4 NULL COMMENT '题目',
  `answer` varchar(10) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '答案',
  `createtime` int(10) NULL DEFAULT NULL COMMENT '创建时间',
  `updatetime` int(10) NULL DEFAULT NULL COMMENT '更新时间',
  `deletetime` int(10) NULL DEFAULT NULL COMMENT '删除时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COMMENT = '问题列表';

CREATE TABLE IF NOT EXISTS `__PREFIX__mental_remark`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `main_id` int(10) NULL DEFAULT NULL,
  `user_id` int(10) NULL DEFAULT NULL,
  `log_id` int(10) NULL DEFAULT NULL,
  `content` text CHARACTER SET utf8mb4 NOT NULL COMMENT '内容',
  `is_audit` tinyint(3) NULL DEFAULT 0 COMMENT '审核:0=未审核,1=已通过',
  `createtime` int(10) NULL DEFAULT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COMMENT = '评价';

CREATE TABLE IF NOT EXISTS `__PREFIX__mental_result`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `main_id` int(10) NULL DEFAULT NULL,
  `component_id` int(10) NULL DEFAULT 0 COMMENT '分量',
  `type` tinyint(1) NULL DEFAULT 1 COMMENT '类型:1=求和,2=求平均',
  `content` text CHARACTER SET utf8mb4 NOT NULL COMMENT '内容',
  `createtime` int(10) NULL DEFAULT NULL COMMENT '创建时间',
  `updatetime` int(10) NULL DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `main_id`(`main_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COMMENT = '测试结果';

CREATE TABLE IF NOT EXISTS `__PREFIX__mental_result_item`  (
  `result_id` int(10) NOT NULL,
  `main_id` int(10) NULL DEFAULT NULL,
  `component_id` int(10) NULL DEFAULT 0 COMMENT '分量',
  `number` int(10) NULL DEFAULT 0 COMMENT '序号',
  `title` varchar(125) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '标题',
  `left` decimal(10, 2) NULL DEFAULT NULL,
  `right` decimal(10, 2) NULL DEFAULT NULL,
  `content` text COMMENT '内容',
  `createtime` int(10) NULL DEFAULT NULL COMMENT '创建时间',
  `updatetime` int(10) NULL DEFAULT NULL COMMENT '更新时间',
  INDEX `result_id`(`result_id`) USING BTREE,
  INDEX `number`(`number`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4;

CREATE TABLE IF NOT EXISTS `__PREFIX__mental_testlog`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(10) NULL DEFAULT NULL,
  `main_id` int(10) NULL DEFAULT NULL,
  `title` varchar(125) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '结果标题',
  `content` text CHARACTER SET utf8mb4 NULL COMMENT '结果内容',
  `type` enum('skip','mark','component') CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '类型:skip=跳转类型,mark=打分类型,component=量表测试',
  `result_log` text CHARACTER SET utf8mb4 NULL COMMENT '提交结果',
  `status` enum('unfinish','finish') CHARACTER SET utf8mb4 NOT NULL DEFAULT 'unfinish' COMMENT '状态:unfinishu=未完成,finish=已完成',
  `createtime` int(10) NULL DEFAULT NULL COMMENT '创建时间',
  `updatetime` int(10) NULL DEFAULT NULL COMMENT '更新时间',
  `deletetime` int(10) NULL DEFAULT NULL COMMENT '删除时间',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `main_id`(`main_id`, `user_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COMMENT = '测试记录';

CREATE TABLE IF NOT EXISTS `__PREFIX__mental_testmain`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `type` enum('skip','mark','component') CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '类型:skip=跳转类型,mark=打分类型,component=量表测试',
  `pay_mode` tinyint(1) UNSIGNED NULL DEFAULT 0 COMMENT '付费模式:0=免费,1=付费查看,2=付费测试',
  `category_id` int(10) NULL DEFAULT NULL COMMENT '分类ID',
  `title` varchar(125) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '标题',
  `description` varchar(255) CHARACTER SET utf8mb4 NOT NULL DEFAULT '' COMMENT '描述',
  `price` decimal(10, 2) NULL DEFAULT 0.00 COMMENT '价格',
  `image` varchar(100) CHARACTER SET utf8mb4 NOT NULL DEFAULT '' COMMENT '图片',
  `content` text CHARACTER SET utf8mb4 NOT NULL COMMENT '介绍',
  `test_num` int(10) UNSIGNED NULL DEFAULT 0 COMMENT '测试数量',
  `createtime` int(10) NULL DEFAULT NULL COMMENT '创建时间',
  `updatetime` int(10) NULL DEFAULT NULL COMMENT '更新时间',
  `switch` tinyint(1) DEFAULT '0' COMMENT '开关',
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `test_num`(`test_num`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COMMENT = '测试管理';

CREATE TABLE IF NOT EXISTS `__PREFIX__mental_testson`  (
  `main_id` int(10) NULL DEFAULT NULL,
  `component_id` int(10) NULL DEFAULT 0 COMMENT '分量',
  `question_id` int(10) NULL DEFAULT NULL,
  `skip` varchar(255) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '跳转',
  `option` varchar(255) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '选项',
  `result` varchar(255) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '结果',
  `createtime` int(10) NULL DEFAULT NULL COMMENT '创建时间',
  `updatetime` int(10) NULL DEFAULT NULL COMMENT '更新时间',
  INDEX `main_id`(`main_id`) USING BTREE,
  INDEX `question_id`(`question_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COMMENT = '测试问题关联';

CREATE TABLE IF NOT EXISTS `__PREFIX__mental_wx_user`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `type` tinyint(1) UNSIGNED NULL DEFAULT 1 COMMENT '登录类型:1=微信小程序,2=微信公众号,3=头条小程序',
  `user_id` int(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT '关联用户表',
  `openid` varchar(50) CHARACTER SET utf8mb4 NULL DEFAULT NULL,
  `session_key` varchar(50) CHARACTER SET utf8mb4 NULL DEFAULT NULL,
  `unionid` varchar(50) CHARACTER SET utf8mb4 NULL DEFAULT NULL,
  `avatar` varchar(255) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '头像',
  `nickname` varchar(50) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '昵称',
  `gender` smallint(1) UNSIGNED NOT NULL DEFAULT 0 COMMENT '性别:0=未知,1=男,2=女',
  `language` varchar(50) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '语言',
  `city` varchar(80) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '城市',
  `province` varchar(80) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '省',
  `country` varchar(80) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '国家',
  `country_code` varchar(20) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '手机号码国家编码',
  `mobile` varchar(20) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '手机号码',
  `createtime` int(10) NULL DEFAULT NULL COMMENT '创建时间',
  `updatetime` int(10) NULL DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `openid`(`openid`) USING BTREE,
  INDEX `user_id`(`user_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COMMENT = '微信用户';

CREATE TABLE IF NOT EXISTS `__PREFIX__mental_component`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) CHARACTER SET utf8mb4 NULL DEFAULT NULL COMMENT '名称',
  `weigh` int(10) NOT NULL DEFAULT 0 COMMENT '权重',
  `createtime` int(10) NULL DEFAULT NULL COMMENT '创建时间',
  `updatetime` int(10) NULL DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COMMENT = '分量表';


ALTER TABLE `__PREFIX__mental_result` ADD COLUMN `component_id` INT(10) DEFAULT 0 COMMENT '分量ID' AFTER `main_id`;
ALTER TABLE `__PREFIX__mental_result` ADD COLUMN `type` TINYINT(10) DEFAULT 1 COMMENT '类型:1=求和,2=求平均' AFTER `component_id`;
ALTER TABLE `__PREFIX__mental_result_item` ADD COLUMN `component_id` INT(10) DEFAULT 0 COMMENT '分量ID' AFTER `main_id`;
ALTER TABLE `__PREFIX__mental_testson` ADD COLUMN `component_id` INT(10) DEFAULT 0 COMMENT '分量ID' AFTER `main_id`;
ALTER TABLE `__PREFIX__mental_testlog` ADD COLUMN `type` ENUM('skip','mark','component') DEFAULT NULL COMMENT '类型:skip=跳转类型,mark=打分类型,component=量表测试' AFTER `content`;

ALTER TABLE `__PREFIX__mental_question` ADD COLUMN  `image` varchar(250) CHARACTER SET utf8mb4 NOT NULL DEFAULT '' COMMENT '图片' AFTER `type`;

ALTER TABLE `__PREFIX__mental_testmain` 
ADD COLUMN `pay_mode` tinyint(1) UNSIGNED NULL DEFAULT 0 COMMENT '付费模式:0=免费,1=付费查看,2=付费测试' AFTER `description`,
ADD COLUMN `switch` tinyint(1) DEFAULT '0' COMMENT '开关' AFTER `test_num`;

ALTER TABLE `__PREFIX__mental_order` ADD COLUMN `type` tinyint(1) UNSIGNED NULL DEFAULT 0 COMMENT '类型:0=付费测试,1=付费查看' AFTER `order_id`;

BEGIN;
INSERT INTO `__PREFIX__mental_config`(`id`, `name`, `title`, `value`, `t_name`, `content`, `type`, `rule`, `tip`, `extend`) VALUES (1, 'shuffling_figure', 'shuffling figure', '/assets/addons/mental/img/focus-1.png', 'shuffling_content', '[{\"title\":\"标题1\",\"page\":\"/page/index/index\"}]', 'shuffling', '', NULL, '');
INSERT INTO `__PREFIX__mental_config`(`id`, `name`, `title`, `value`, `t_name`, `content`, `type`, `rule`, `tip`, `extend`) VALUES (2, 'hotkeyword', 'Hot keywords', '爱情,性格,社会', '', '', 'string', '', NULL, '');
INSERT INTO `__PREFIX__mental_config`(`id`, `name`, `title`, `value`, `t_name`, `content`, `type`, `rule`, `tip`, `extend`) VALUES (3, 'phone', 'phone', '18333333333', '', '', 'string', '', NULL, '');
INSERT INTO `__PREFIX__mental_config`(`id`, `name`, `title`, `value`, `t_name`, `content`, `type`, `rule`, `tip`, `extend`) VALUES (4, 'logo', 'Logo', '/assets/img/qrcode.png', '', '', 'image', '', NULL, '');
COMMIT;
